<template>
  <div  class="app-container" >
      <el-form
        :model="queryParams"
        ref="queryRef"
        v-show="showSearch"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="" prop="type">
          <el-select
            v-model="queryParams.type"
            placeholder="担保类型"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in loanType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in statuesType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
          :columns="columns"
        ></right-toolbar>
      </el-row>
  
      <!-- 表格数据 -->
      <el-table
      border 
        v-loading="loading"
        :data="productList"    >
        <el-table-column label="排序"    align="center" prop="roleSort" width="120">
          <template #default="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="产品编号"  align="center" prop="id" v-if="columns[0].visible" />
        <el-table-column
          label="产品名称"
          prop="name"
          align="center"
          :show-overflow-tooltip="true"
          v-if="columns[1].visible"
        />
        <el-table-column
          label="担保类型"
          prop="typeName"
          align="center"
          v-if="columns[2].visible"
        />
        <el-table-column
          label="标签"
          prop="label"
          align="center"
          :show-overflow-tooltip="true"
          v-if="columns[3].visible"
        >
        <template #default="scope">
           <div>新品：{{scope.row.labelNew}}</div>
           <div>推荐：{{scope.row.labelRecomed}}</div>
           <!-- <div>精品：{{scope.row.labelBout}}</div> -->
        </template>
      </el-table-column>
        <el-table-column
          label="状态"
          :show-overflow-tooltip="true"
          v-if="columns[4].visible"
          align="center"
          width="100"
        >
        <template #default="scope">
           <div>{{scope.row.statusName}}</div>
           <el-link type="success" class="stateup" @click="updateState(scope.row,'上架')" v-if="scope.row.statusName=='已审核'||scope.row.statusName=='已下架'">上架</el-link>
           <el-link type="danger" class="stateup" @click="updateState(scope.row,'下架')" v-if="scope.row.statusName=='发布中'">下架</el-link>
        </template>
      
      </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tooltip content="查看" placement="top">
              <el-button
                link
                type="primary"
                icon="View"
                @click="handleView(scope.row,'查看')"
              ></el-button>
            </el-tooltip>
            <el-tooltip 
            v-if="scope.row.status!= 3 && scope.row.status != 4"
            content="编辑​" placement="top">
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleView(scope.row,'编辑')"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              content="提审"
              placement="top"
              v-if="scope.row.status == 2"
            >
              <el-button
                link
                type="primary"
                icon="CircleCheck"
                @click="handleUpdateStatus(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              content="删除"
              placement="top"
              v-if="scope.row.status !== 4"
            >
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page_size"
        v-model:limit="queryParams.page_count"
        @pagination="getList"
      />
    </div>
</template>

<script setup name="Product">
import {
  listProduct,
  delProduct,
  stateProduct,
} from "@/api/product/product";
import {getOrg} from "@/utils/auth"
const router = useRouter();
const { proxy } = getCurrentInstance();
//类型
const loanType = ref([
  {
    value: "1",
    label: "个人信用",
  },
  {
    value: "2",
    label: "个人分期",
  },
  {
    value: "3",
    label: "个人抵押",
  },
  {
    value: "4",
    label: "企业信用",
  },
  {
    value: "5",
    label: "企业分期",
  },
  {
    value: "6",
    label: "企业租赁",
  },
]);
//状态
const statuesType = ref([
  {
    value: "",
    label: "全部",
  },
  {
    value: "1",
    label: "未审核",
  },
  {
    value: "2",
    label: "审核未通过",
  },
  {
    value: "3",
    label: "已审核",
  },
  {
    value: "4",
    label: "发布中",
  },
  {
    value: "5",
    label: "已下架",
  },
]);
// 列显隐信息
const columns = ref([
  { key: 0, label: `产品编号`, visible: true },
  { key: 1, label: `产品名称`, visible: true },
  { key: 2, label: `担保类型`, visible: true },
  { key: 3, label: `标签`, visible: true },
  { key: 4, label: `状态`, visible: true },
]);
const productList = ref([]);//担保产品数据
const loading = ref(false);
const showSearch = ref(true);//是否展示搜索
const total = ref(0);//总条数
const data = reactive({
  form: {},
  queryParams: {
    page_size: 1,
    page_count: 10,
    status: undefined,
    type: undefined,
    org_id:getOrg().id
  }
});

const { queryParams} = toRefs(data);

/** 查询担保产品列表 */
function getList() {
  loading.value = true;
  listProduct(queryParams.value).then((response) => {
    loading.value = false;
    productList.value = [];
    if (response.data.total_page > 0) {
      productList.value = response.data.list.map((item) => {
        item.typeName = loanType.value.filter((itm) => {
          return itm.value == item.type;
        })[0].label;
        if(item.status){
          item.statusName = statuesType.value.filter((tm) => {
            return tm.value == item.status;
          })[0].label;
        }else{
          item.statusName = '未审核'
          item.status ='1'
        };
        if(item.label){
             item.labelNew = item.label.indexOf('新品')!=-1?'是':'否';
             item.labelRecomed = item.label.indexOf('推荐')!=-1?'是':'否';
             item.labelBout = item.label.indexOf('精品')!=-1?'是':'否';
        }else{
            item.labelNew = '否';
             item.labelRecomed = '否';
             item.labelBout = '否';
        }
        return item;
      });
    } else {
      productList.value = [];  
    }
    total.value = productList.value.length;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_size = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增 */
function handleAdd(){
  router.push({
    path:"/product-factory/addMaterial",
    query:{
      viewType:'新增',
      ids:"",
    }
  })
}
getList();
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('确定要删除该产品吗？')
    .then(function () {
      return delProduct(row.id);
    })
    .then((response) => {
      if(response.code=='200'&& response.data){
        getList();
        proxy.$modal.msgSuccess("删除成功");
      }else{
        proxy.$modal.msgWarning(response.msg);
      }
    })
    .catch(() => {});
}
/**修改状态（上架、下架）*/
function updateState(row,name){
  const status = name=='上架'?'4':'5';
  proxy.$modal
    .confirm('确定要'+name+'该产品吗？')
    .then(function () {
      return  stateProduct(row.id,status);
    })
    .then((response) => {
      if(response.code=='200'&& response.data){
         getList();
         proxy.$modal.msgSuccess(name+"成功");
      }else{
        proxy.$modal.msgWarning(response.msg);
      }
    })
    .catch(() => {});
}  
/** 提交审核 */
function handleUpdateStatus(row) {
  proxy.$modal
    .confirm('确定提交产品审核？')
    .then(function () {
      return stateProduct(row.id,'1');
    })
    .then((response) => {
      if(response.code=='200'&& response.data){
        getList();
        proxy.$modal.msgSuccess("提审成功");
      }else{
        proxy.$modal.msgWarning(response.msg);
      }
    })
    .catch(() => {});
}
/**查看  编辑*/
function handleView(row,val){
  router.push({
    path:"/product-factory/addMaterial",
    query:{
      viewType:val,
      ids: row.id,
      name:row.name
    }
  })
};
onActivated(()=>{
  let timer = setTimeout(()=>{
     getList();
     clearTimeout(timer)
   },1100)
 })
</script>
<style>
</style>
